# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=wezterm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=20240203.110809.5046fc22
pkgver=20240203.110809.5046fc22+r325+g30345b36d
pkgrel=3
pkgdesc="A GPU-accelerated cross-platform terminal emulator and multiplexer (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://wezfurlong.org/wezterm/"
msys2_repository_url='https://github.com/wez/wezterm'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-libssh2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             'git')
source=("git+${msys2_repository_url}.git#commit=30345b36d8a00fed347e4df5dadd83915a7693fb"
        "mingw-build.patch")
sha256sums=('89822773f12af00cb88ac6af76567fac97b543f4cacaa197c47d04eb228eeb6b'
            '7cc2ff9d4214d7fc51799964513b3b4a1d285d9ae36c44e6503f8cc6195336e9')

pkgver() {
  cd "${_realname}"

  printf "%s+r%s" "${_basever}" "$(git describe --tags | sed 's|[^-]*-[^-]*-||;s|[^-]*-||;s|-|+|g')"
}

prepare() {
  cd "${_realname}"

  patch -Np1 -i "${srcdir}/mingw-build.patch"

  git submodule update --init --recursive
  cargo update -p cc
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  # Not compatible with libgit2 1.8+
  # export LIBGIT2_NO_VENDOR=1
  export OPENSSL_NO_VENDOR=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export LIBSSH2_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  cargo build --release --frozen --features distro-defaults
}

package() {
  cd "${_realname}"

  install -Dm755 target/release/wezterm{,-gui,-mux-server} -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm755 target/release/strip-ansi-escapes -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm644 LICENSE.md -t "${pkgdir}${MINGW_PREFIX}/share/licenses/wezterm"
  install -Dm644 assets/shell-completion/bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/wezterm"
  install -Dm644 assets/shell-completion/fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/wezterm.fish"
  install -Dm644 assets/shell-completion/zsh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_wezterm"
}
